 <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../res/layui/layui.js"></script>
    <script src="../res/js/xlk.js"></script>
    <link rel="stylesheet" href="../res/layui/css/layui.css" media="all">
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/zTree.v3/3.5.33/js/jquery.ztree.core.min.js"></script>
    <link href="https://cdn.bootcss.com/zTree.v3/3.5.33/css/zTreeStyle/zTreeStyle.min.css" rel="stylesheet">
    <script type="text/javascript">
        //全局同步
        $.ajaxSetup({
            async: false
        });
        function getQueryString(name){
            var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if(r!=null)return  unescape(r[2]); return null;}
        var id = getQueryString("id");
    </script>
</head>
<body>
<form class="layui-form" action="" id="AtsBaseItem">
    <input type="hidden" name="itemId" id="itemId"/>
    <input type="hidden" name="ids" id="ids">
    <div class="layui-form-item">
        <label class="layui-form-label">指标名称</label>
        <div class="layui-input-block">
            <input type="text" name="itemExp" id="itemExp" required
                   lay-verify="required" placeholder="请输入角色名称" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">指标单位</label>
        <div class="layui-input-block">
            <input type="text" name="itemUnit" id="itemUnit" required
                   lay-verify="required" placeholder="请输入角色名称" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">指标层级</label>
        <div class="layui-input-block">
            <input type="text" name="itemLevel" id="itemLevel" required
                   lay-verify="required" placeholder="请输入角色名称" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">创建人</label>
        <div class="layui-input-block">
            <input type="text" name="creater" id="creater" required
                   lay-verify="required" placeholder="请输入角色名称" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <input type="text" name="remark" id="remark" required
                   lay-verify="required" placeholder="请输入角色名称" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">体育项目：</label>
        <div class="layui-input-block" id="fxk">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">上级指标:</label>
        <div class="layui-input-block" style="width: 300px;">
            <input id="orgName" name="orgName" class="layui-input" onclick="showTree()" placeholder="不选为根模型" autocomplete="off" width="100px" lay-verify="title" readonly>
            <input type="hidden" value="0" name="parentId" id="parentId">
            <div class="ztree"  style="display:none;position: relative;position: absolute;width:290px;border: 1px solid #e6e6e6;background-color: #ffffff">
                <ul id="treeDemo">

                </ul>
            </div>
        </div>
        <div style="margin-left: 110px;margin-top: 10px;">
            <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<script>
    //Demo    $(function(){})
    var form = null ;
    layui.use('form', function(){
        var rolesId = "";
        form = layui.form;
        //监听提交
        form.on('submit(formDemo)', function(data){
            $("input[name='roles']:checked").each(function(index,item){
                rolesId+=$(this).val()+",";
            });
            $("#ids").val(rolesId);
            //layer.msg(JSON.stringify(data.field));
            var formData = new FormData(document.getElementById("AtsBaseItem"));
            var url = "../AtsBaseItem/ljzj";
            $.ajax({
                url:url,
                type:"post",
                data:formData,
                processData:false,
                contentType:false,
                dataType:"json",
                success:function(obj){
                    if(obj.num==1){
                        layer.msg(obj.msg, {
                            icon: 1,
                            time: 1000 //1秒关闭（如果不配置，默认是3秒）
                        }, function(){
                            //关闭当前层
                            //当你在iframe页面关闭自身时
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.layer.close(index); //再执行关闭
                            //刷新父页面
                            //parent.location.reload();
                        });
                    }else{
                        layer.msg(obj.msg);
                    }
                },
                error:function(e){
                    layer.msg("操作错误!");
                }
            });
            return false;
        });
        findAll();
        xlkfz();
        fxkfz();
        fxkzf();
    });
    function findAll(){
        //将下拉框新闻类型赋值
        var url = "../AtsBaseItem/findId";
        var id = getQueryString("id");
        var data = {id:id};
        //jquery的ajax方法,查询新闻类型的方法
        $.get(url,data,function(obj){
            //从后台传过来的数据obj
            $("#creater").val(obj.creater);
            $("#itemId").val(obj.itemId);
            $("#itemExp").val(obj.itemExp);
            $("#itemUnit").val(obj.itemUnit);
            $("#itemLevel").val(obj.itemLevel);
            $("#parentId").val(obj.parentId);
            $("#remark").val(obj.remark);
            var url1 = "../AtsBaseItem/findId";
            var data1 = {id:obj.parentId};
            $.get(url1,data1,function(obj1){
                $("#orgName").val(obj1.itemExp);
                $("#parentId").val(obj1.itemId);
            },"json");
            //重新渲染
            form.render();
        },"json");
    }
    function xlkfz() {
        var orgList = [];
        var jsonDataTree1 = [];
        var url = "../AtsBaseItem/selectAll";
        var data = {};
        $.get(url, data, function (obj) {
            //从后台传过来的数据obj$("[value='11']").prop("checked",true);
            $.each(obj, function (index, item) {
                orgList.push({id: item.itemId, pId: item.parentId, name: item.itemExp}); //挨个取出类别并填入类别数组
            });
            $.fn.zTree.init($("#treeDemo"), setting, orgList);
        }, "json");
    }
    function fxkfz() {
        var url = "../AtsBaseSport/selected";
        var data = {};
        $.get(url, data, function (obj) {
            $.each(obj, function (index, item) {
                $("#fxk").append("<input type='checkbox' name='roles' id='"+item.sportId+"' value='"+item.sportId+"' title='"+item.sportName+"'>");
                form.render();
            });
        }, "json");
    }
    function fxkzf() {
        var id = getQueryString("id");
        var url = "../AtsBaseItem/faffs";
        var data = {id:id};
        $.get(url, data, function (obj) {
            $.each(obj, function (index, item) {
                $("#"+item.sportId).prop("checked",true);
            });
            form.render();
        }, "json");
    }
</script>
</body>
</html>